package com.sduept.nwld.dataview.controller.message;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.core.dao.SuperDAO;
import com.sduept.message.entity.Message;
import com.sduept.nwld.dataserver.manager.message.UserMessageManager;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.utils.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Named
@ViewScoped
public class LookAllMessageController extends AbstractController {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1021954886125757503L;

	public  static final String libpath = "com.sduept.component.message.model.";

	private Class<? extends Message> T = null;
	@Autowired
	private SuperDAO dao;
	
	@Autowired
	private UserMessageManager mm;
	
	private String className;
	
	private LazyDataModel<Message> model;
	
	private List<Message> records ;
	private String esql;
	private Date queryStartDate;
	private Date queryEndDate;
	private String content;
	private PersonnelEntityBean user;
	@SuppressWarnings("unchecked")
	@PostConstruct
	public void init(){
		user = getCurrentUser();
		className = getParameter("ClassName");
		String cname = libpath+className;
		try {
			T = (Class<? extends Message>) Class.forName(cname);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		model = new LazyDataModel<Message>() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 8288412715384289085L;

			@Override
			public List<Message> load(int first, int pageSize,
					String sortField, SortOrder sortOrder,
					Map<String, Object> filters) {
				if(user!=null)
					esql = "target='"+user.getCode()+"'";
				records = mm.findMessageRecordByCondition(esql,T, " order by createTime", new int[]{first,first+pageSize});
				return records;
			}
			
		};
		if(StringUtils.isEmpty(esql)){
			model.setRowCount(mm.countMessageRecords("target='"+user.getCode()+"'",T));
		}else{
			model.setRowCount(mm.countMessageRecords(esql,T));
		}
	}
	public void query(){
		esql = null;
		genEsql();
		model.setRowCount(mm.countMessageRecords(esql,T));
	}
	private void genEsql(){
		List<String> map = new ArrayList<String>();
		if(queryStartDate!=null){
			map.add(" createTime >= '"+DateUtil.getStringDate(queryStartDate,DateUtil.pattern_ymd)+"'");
		}
		if(queryEndDate!=null){
			map.add(" createTime <='"+DateUtil.getStringDate(queryEndDate,DateUtil.pattern_ymd)+"'");
		}
		if(!StringUtils.isEmpty(content)){
			map.add("content like '%"+content+"%'");
		}
		for(String s : map){
			if(esql == null){
				esql =  s;
			} else {
				esql += " AND "+s;
			}
		}
	}
	public LazyDataModel<? extends Message> getModel() {
		return model;
	}
	public Date getQueryStartDate() {
		return queryStartDate;
	}
	public void setQueryStartDate(Date queryStartDate) {
		this.queryStartDate = queryStartDate;
	}
	public Date getQueryEndDate() {
		return queryEndDate;
	}
	public void setQueryEndDate(Date queryEndDate) {
		this.queryEndDate = queryEndDate;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	
}
